from alg import Solution
import matplotlib.pyplot as plt
import time
import random
import numpy as np

def test_super_washing_machine():
    solution = Solution()
    
    # 测试用例1：基本测试
    test1 = [1, 0, 5]
    print("测试用例1:", test1)
    print("预期结果: 3")
    print("实际结果:", solution.findMinMoves(test1))

    
    # 测试用例2：无法平均分配的情况
    test2 = [0, 2, 0]
    print("测试用例2:", test2)
    print("预期结果: -1")
    print("实际结果:", solution.findMinMoves(test2))



    # 测试用例3：已经平均的情况
    test3 = [2, 2, 2]
    print("测试用例3:", test3)
    print("预期结果: 0")
    print("实际结果:", solution.findMinMoves(test3))

    
    # 测试用例4：边界情况 - 空列表
    test4 = []
    print("测试用例4:", test4)
    print("预期结果: 0")
    print("实际结果:", solution.findMinMoves(test4))


    # 测试用例5：复杂情况
    test5 = [4, 0, 0, 4]
    print("测试用例5:", test5)
    print("预期结果: 2")
    print("实际结果:", solution.findMinMoves(test5))

    # 算法性能测试  
    length = [400, 800, 1600, 3200, 6400, 12800, 25600, 51200, 102400]
    times = []
    for i in range(len(length)):
        
        machines = [random.randint(0, 100) for _ in range(i-1)]
        #保证每次生成的结果都是可以平均分配的
        last_num = length[i] - sum(machines) % length[i]
        machines.append(last_num)
        
        #machines = [2 for _ in range(length[i])]
        start_time = time.perf_counter()
        solution.findMinMoves(machines)
        end_time = time.perf_counter()
        times.append(end_time - start_time)
        print(f"长度为{length[i]}的测试用例，运行时间: {end_time - start_time}秒")

    #理论曲线
    n = np.array(length)
    n_log_n = n * np.log2(n + 1)
    n2 = n ** 2

    #归一化
    def normalize(ref, base):
        return [x / ref[0] * base[0] for x in ref]
    
    #图表绘制
    plt.figure(figsize=(10,6))
    plt.plot(length, times, 'bo-', label='Measured Time', linewidth = 2)
    plt.plot(length, normalize(n, times),'r--', label='O(n)',linewidth = 2)
    plt.plot(length, normalize(n_log_n, times),'g--', label='O(n log n)',linewidth = 2)
    plt.plot(length, normalize(n2, times),'m--', label='O(n²)',linewidth = 2)

    plt.xlabel('Length of input list')
    plt.ylabel('Time (seconds)')
    plt.title('Super Washing Machine Performance Analysis')
    plt.grid(True)
    plt.legend()
    plt.tight_layout()
    plt.show()
    plt.savefig('super_washing_machine.png')

if __name__ == "__main__":
    test_super_washing_machine()
